package com.aop;

import com.util.SessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class TransectionAspect {


//    @Before("execution(* com.com.service.*.*(..))")
//    public void before(){
//        System.out.println("我在所有方法之前执行----------------前置通知");
//    }

    @Around("@annotation(transaction)") //环绕增强，相当于MethodInterceptor
    public Object go(ProceedingJoinPoint pj,Transaction transaction){
        Object obj=null;
        SqlSession session= SessionUtil.getSession();
        try {
            obj=pj.proceed(); //调用原来的方法
            System.out.println(pj.proceed());
            session.commit();

        }catch (Throwable e){
            e.printStackTrace();

            session.rollback();
        }finally {
            session.close();

        }
        return obj;

    }
    @After("execution(* com.service.*.*(..))")
    public void after(){
        System.out.println("我在所有方法之前执行---------------后置通知");

    }


}
